﻿var workUtils = {
	params : {},
	rootPath : "",
	createTable : function(json) {
		var index = layer.load(1);
		var params = json.params || {};
		params["pageNo"] = json["pageNo"];
		params["pageSize"] = json["pageSize"];
		$.ajax({
			url : json["url"],
			async : false,
			type : 'post',
			dateType : 'json',
			data : params,
			success : function(result) {
				var obj = result;
				var count = obj.count;
				var data = obj.list;
				layer.close(index);
				//计算总页数（一般由后台返回）
				json.render(data);
				json.laypage.render({
					elem : json["laypageId"],
					count : count,
					limit : json["pageSize"],
					page : json["pageNo"],
					layout : [ 'count', 'prev', 'page', 'next', 'limit', 'skip' ],
					jump : function(obj, first) {
						//首次不执行
						if (!first) {
							json["pageNo"] = obj.curr;
							json["pageSize"] = obj.limit;
							tableUtils.create(json);
						}
					}
				});
			}
		});
		return this;
	},
	toDate : function(time, format) {
		format = format || "yyyy-MM-dd";
		return new Date(time).Format(format);
	},
	open : function(json, callback) {
		json["width"] = json["width"] || "480px";
		json["height"] = json["height"] || "470px";
		json["offset"] = json["offset"] || "auto";
		$(".layui-my-window").remove();
		$.get(json["url"], function(result) {
			layer.open({
				type : 1,
				title : json["title"] || "",
				area : [ json["width"], json["height"] ],
				anim : 2,
				shade : 0, //noshow
				offset : json["offset"],
				skin : 'layui-my-window',
				content : result,
				zIndex : layer.zIndex, 
				success : function(layero) {
					layer.setTop(layero);
					$(".layui-layer-close").html("&#x1006;");
					$(".layui-layer-close").removeClass("layui-layer-ico");
					$(".layui-layer-close").addClass("layui-icon");
				},
				end : function(index) {
					if (callback) {
						callback();
					}
				}
			});
		});
	},
	popup : function(json, callback) {
		json["width"] = json["width"] || "480px";
		json["height"] = json["height"] || "470px";
		$.get(json["url"], function(result) {
			layer.open({
				id : "layer-msg",
				type : 1,
				area : [ json["width"], json["height"] ],
				anim : 1,
				skin : 'pm-layer-login',
				content : result,
				end : function(index) {
					if (callback) {
						callback();
					}
				}
			});
		});
	},
	tableHeight : function(id, hei) {
		var obj = $(id).find(".layui-table-body");
		obj.height(obj.height() + hei);
	},
	html : function(json) {
		var id = json["id"] || "layui-body";
		$.ajax({
			type : 'get',
			url : json["url"],
			dataType : "html",
			success : function(result) {
				$("#" + id).html(result);
			}
		});

	},
	fillForm : function($form, json) {
		var jsonObj = json;
		if (typeof json === 'string') {
			jsonObj = $.parseJSON(json);
		}

		for (var key in jsonObj) { //遍历json字符串
			var str = jsonObj[key];
			var tagobjs = $("[name=" + key + "]", $form);
			if ($(tagobjs[0]).attr("type") == "radio") { //如果是radio控件
				$.each(tagobjs, function(keyobj, value) {
					if ($(value).attr("val") == jsonObj[key]) {
						value.checked = true;
					}
				});
				continue;
			}
			$("[name=" + key + "]", $form).val(jsonObj[key]);

		}
	},
	clearForm : function($form, cols) {
		for (var i = 0; i < cols.length; i++) { //遍历json字符串
			$("[name=" + cols[i] + "]", $form).val("");
		}
	},
	setCkb : function(name, value) {
		//alert(name + " " + value);
		//$("[name=" + name + "][value=" + value + "]").attr("checked", "checked");  不知为何找不到具体标签;
		$("[name=" + name + "][val=" + value + "]").attr("checked", "checked");
	},
	buildMenu : function(id, menus) {
		var html = "";
		html += '<div class="layui-col-md6 layui-table-cell menu-bars">';
		for (var key in menus) {
			html += '<a  data-id="' + id + '" class="layui-btn layui-btn-xs action-' + key + '">' + menus[key] + '</a> ';
		}
		html += '</div>';
		return html;
	},
	buildMenu1 : function(id, menus) {
		var html = "";
		html += '<div class="menu-bars">';
		html += '<i class="layui-icon layui-icon-menu" style="font-size: 20px; color: #1E9FFF;">&#xe671;</i>  ';
		html += '<dl class="widget-menu">';
		for (var key in menus) {
			if (key == 'split') {
				html += '<dd  class="action-split"></dd> ';
			} else {
				html += '<dd  data-id="' + id + '" class="action-' + key + '">' + menus[key] + '</dd> ';
			}
		}
		html += '</dl></div>';
		return html;
	},
	put : function(key, value) {
		this.params[key] = value;
	},
	get : function(key) {
		var value = this.params[key];
		return value;
	},
	msg : function(result) {
		layer.msg(result.msg, {
			time : 1500
		});
		if (result.status) {
			layer.closeAll();
		}
	},
	ajaxGet : function(url, data, callback) {
		$.ajax({
			url : workUtils.rootPath + url,
			async : false,
			type : 'get',
			dateType : 'json',
			data : data,
			success : function(data) {
				callback(data);
			}
		});
	},
	ajaxPost : function(url, data, callback) {
		$.ajax({
			url : workUtils.rootPath + url,
			async : true,
			type : 'post',
			dateType : 'json',
			data : data,
			success : function(data) {
				callback(data);
			}
		});
	},
	ajaxPostLog : function(data) {
		var getUrlParam = function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        };
        var user = getUrlParam("accId");
        if(user==null){
        	user = getUrlParam("loginUser");
        }
        data.subUser = user;
		$.ajax({
			url : "http://10.224.206.62:18999/noasweb_test/logGeneral",
			type : 'post',
			dateType : 'json',
			contentType: "application/x-www-form-urlencoded; charset=utf-8", 
			data : data,
			success : function(data) {
				console.log(data);
			}
		});
	},
	switchCut : function(left, id) {
		var last = null;
		var flag = false;
		if (left) {
			$("#" + id + " .simple-body-cut").each(function() {
				if (flag) {
					return;
				}
				var show = $(this).is(':visible');
				if (show) {
					if (last == null) {
						flag = true;
					} else {
						$("#" + id + " .simple-body-cut").hide();
						last.show();
					}
				}
				last = $(this);
			});
		} else {
			$("#" + id + " .simple-body-cut").each(function() {
				if (flag) {
					last = $(this);
					$("#" + id + " .simple-body-cut").hide();
					last.show();
					return;
				}
				var show = $(this).is(':visible');
				if (show) {
					flag = true;
				}
			});
		}
	},
	onclick : function(id, callback) {
		$(document).off("click", id);
		$(document).on("click", id, function(event) {
			event.stopPropagation();
			if (callback) {
				callback(this);
			}
		});
	},
	getUrlParam:function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    },
	nowTime:function(){
		return new Date().Format("yyyy-MM-dd hh:mm:ss"); 
	}
};
/*
 js由毫秒数得到年月日
 */
Date.prototype.Format = function(fmt) { //author: meizz
	var o = {
		"M+" : this.getMonth() + 1, //月份
		"d+" : this.getDate(), //日
		"h+" : this.getHours(), //小时
		"m+" : this.getMinutes(), //分
		"s+" : this.getSeconds(), //秒
		"q+" : Math.floor((this.getMonth() + 3) / 3), //季度
		"S" : this.getMilliseconds() //毫秒
	};
	if (/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
};

String.prototype.startWith = function(s) {
	if (s == null || s == "" || this.length == 0 || s.length > this.length)
		return false;
	if (this.substr(0, s.length) == s)
		return true;
	else
		return false;
	return true;
};